package com.lh.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LuoHang
 * @Date 2022-08-26 00:59
 **/
public class BeanCopyUtils {
    private BeanCopyUtils(){}

    public static <T> T copyBean(Object source,Class<T> clz){
        T result = null;
        try{
            result = clz.newInstance();
            BeanUtils.copyProperties(source,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public static <T> List<T> copyBeans(List<?> sources,Class<T> clz){
        return sources.stream()
                .map(source -> copyBean(source,clz))
                .collect(Collectors.toList());
    }
}
